package edu.cebanc.spring.biblioteca.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.bean.AuthorBean;
import edu.cebanc.spring.biblioteca.bean.GenreBean;
import edu.cebanc.spring.biblioteca.bean.LanguageBean;
import edu.cebanc.spring.biblioteca.bean.PublisherBean;
import edu.cebanc.spring.biblioteca.dao.AuthorsDAO;
import edu.cebanc.spring.biblioteca.dao.GenreDAO;
import edu.cebanc.spring.biblioteca.dao.LanguagesDAO;
import edu.cebanc.spring.biblioteca.dao.PublishersDAO;
import edu.cebanc.spring.biblioteca.util.DataParser;

@Service("genDataService")
@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
public class GeneralDataServiceImpl implements GeneralDataService {

	@Autowired
	private AuthorsDAO authorsDAO;
	@Autowired
	private LanguagesDAO languagesDAO;
	@Autowired
	private GenreDAO genreDAO;
	@Autowired
	private PublishersDAO publishersDAO;
	
	public List<AuthorBean> listAuthors() {
		DataParser parser = new DataParser();
		return parser.AuthorList2AuthorBeanList(authorsDAO.listAuthors());
	}
	public List<LanguageBean> listLanguages()  {
		DataParser parser = new DataParser();
		return parser.LanguageList2LanguageBeanList(languagesDAO.listLanguages());
	}
	public List<GenreBean> listGenres() {
		DataParser parser = new DataParser();
		return parser.GenreList2GenreBeanList(genreDAO.listGenres());
	}
	public List<PublisherBean> listPublishers() {
		DataParser parser = new DataParser();
		return parser.PublisherList2PublisherBeanList(publishersDAO.listPublishers());
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void saveAuthor(AuthorBean author) {
		DataParser parser = new DataParser();
		authorsDAO.saveAuthor(parser.AuthorBean2Author(author));		
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void saveLanguage(LanguageBean lang) {
		DataParser parser = new DataParser();
		languagesDAO.saveLanguage(parser.LanguageBean2Language(lang));
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void saveGenre(GenreBean genre) {
		DataParser parser = new DataParser();
		genreDAO.saveGenre(parser.GenreBean2Genre(genre));
		
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void savePublisher(PublisherBean pub) {
		DataParser parser = new DataParser();
		publishersDAO.savePublisher(parser.PublisherBean2Publisher(pub));
		
	}
	@Override
	public AuthorBean loadAuthor(String id) {
		DataParser parser = new DataParser();
		return parser.Author2AuthorBean(authorsDAO.getAuthor(Integer.parseInt(id)));
	}
	@Override
	public LanguageBean loadLanguage(String id) {
		DataParser parser = new DataParser();
		return parser.Language2LanguageBean(languagesDAO.getLanguage(Integer.parseInt(id)));
	}
	@Override
	public GenreBean loadGenre(String id) {
		DataParser parser = new DataParser();
		return parser.Genre2GenreBean(genreDAO.getGenre(Integer.parseInt(id)));
	}
	@Override
	public PublisherBean loadPublisher(String id) {
		DataParser parser = new DataParser();
		return parser.Publisher2PublisherBean(publishersDAO.getPublisher(Integer.parseInt(id)));
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void deleteAuthor(String id) {
		authorsDAO.delAuthor(authorsDAO.getAuthor(Integer.parseInt(id)));
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void deleteLanguage(String id) {
		languagesDAO.delLanguage(languagesDAO.getLanguage(Integer.parseInt(id)));
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void deleteGenre(String id) {
		genreDAO.delGenre(genreDAO.getGenre(Integer.parseInt(id)));
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void deletePublisher(String id) {
		publishersDAO.delPublisher(publishersDAO.getPublisher(Integer.parseInt(id)));
	}
	
}
